// addManage.js
var app = getApp()
Page({

  data: {
   
  },
  //添加地址
  addAddrClick : function(){
    wx.redirectTo({
      url: '../addrAditor/addrAditor?type=1',
    })
  },
  //地址编辑点击按钮
  adatorClick : function(e){
    var id = e.currentTarget.dataset.id;
    var thiz = this

    thiz.data.addrData.forEach(function (element, index) {
      if (element.address_id == id) {
        
        wx.redirectTo({
          url: '../addrAditor/addrAditor?address_id=' + element.address_id,
        })
      }
    })
  },
  //地址删除点击按钮
  deleteClick : function(e){
    var id = e.currentTarget.dataset.id;
    var thiz = this

    thiz.data.addrData.forEach(function (element, index) {
      if (element.address_id == id) {
        wx.showModal({
          title: '提示',
          content: '是否删除此地址',
          success : function(res){
            if(res.confirm){
              thiz.setData({
                hiddenLoading : false
              })
              var domain = app.globalData.domain
              var addrDelUrl = app.globalData.addrDelInterface
              var addrDel = domain + addrDelUrl;

              var addrListUrl = app.globalData.addrInfoInterface         
              var addrList = domain + addrListUrl;
              var user_id = app.globalData.inv_i

              wx.request({
                url: addrDel,
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data: {
                  address_id: element.address_id,
                  model: "MINI",
                  user_id: user_id
                },
                success : function(res){
                 
                  wx.request({
                    url: addrList,
                    method: 'POST',
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    data: {
                      format_type: 'json',
                      api_cate: 'Personal',
                      api: 7002,
                      model: "MINI",
                      user_id: user_id
                    },
                    success: function (res) {
                      if (res.data.data.length > 0) {
                        thiz.setData({
                          addrData: res.data.data,
                          hiddenLoading: true
                        })
                      } else {
                        thiz.setData({
                          addrData: [],
                          hiddenLoading: true
                        })
                      }
                    }
                  })


                }
              })
            }
          }
        })
      }
    })
  },
  //checkbox点击事件
  checkboxClick : function(e){
    var id = e.currentTarget.dataset.id;
    var thiz = this

    thiz.data.addrData.forEach(function(element, index){
      element.defaultStatus = false;
      if (element.id == id) {
          element.defaultStatus = !element.defaultStatus;
      }
     
    })

    thiz.setData({
      addrData : thiz.data.addrData
    })
  },

  morenClick:function(e){
    var id = e.currentTarget.dataset.id;
    var domain = app.globalData.domain;
    var pageUrl = app.globalData.PageInterface;
    var morenURL = domain + pageUrl;
    var user_id = app.globalData.inv_i
    var thiz = this;
    wx.request({
      url: morenURL,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Personal',
        api: 7007,
        model: "MINI",
        user_id: user_id,
        address_id:id
      },
      success:function(res){
        if(res.data.status==1){
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 3000
          })
          wx.navigateBack({
            delta: 1
          })
        }
      }
    });

  },


  onShow : function(options){
    var thiz = this
    var domain = app.globalData.domain
    var addrListUrl = app.globalData.addrInfoInterface
    var cid = app.globalData.cid
    var kui_key = app.globalData.kui_key

    var addrList = domain + addrListUrl
    var user_id = app.globalData.inv_i

    wx.request({
      url: addrList,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Personal',
        api: 7002,
        model: "MINI",
        user_id: user_id
      },
      success : function(res){
        if(res.data.data.length > 0){
          thiz.setData({
            addrData: res.data.data,
            hiddenLoading : true
          })
        }else{
          thiz.setData({
            addrData: [],
            hiddenLoading: true
          })
        }
      }
    })
  },
  onLoad: function (options) {
    //获取系统信息
    var thiz = this;

    var background_color = app.globalData.background_color

    wx.setNavigationBarColor({
      backgroundColor: background_color,
    })

    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          hiddenLoading: false,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
        });
      }
    })
  }
})